﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mmoss.orm;
using mmoss.application.classes;

namespace mmoss.application
{
    public partial class frmVendor : Form
    {
        Vendor SelectedVendor { get; set; }
        public frmVendor()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmVendor_Load);
        }

        void frmVendor_Load(object sender, EventArgs e)
        {
            Vendor l_test = new Vendor();
            //load the combo
            cboVendorList.DataSource = dbConn.db.Vendors;
            cboVendorList.DisplayMember = "vendorName";
            cboVendorList.ValueMember = "vendorID";
            cboVendorList.SelectedIndex = -1;
            cboVendorList.SelectedIndexChanged += new EventHandler(cboVendorList_SelectedIndexChanged);
        }

        void cboVendorList_SelectedIndexChanged(object sender, EventArgs e)
        {
            //set the current vendor object
            SelectedVendor = ((Vendor)cboVendorList.SelectedItem);
            //load vendor info
            loadVendorForm();
        }

        private void loadVendorForm()
        {
            txtContactEmail.Text = (SelectedVendor == null ? "" : SelectedVendor.vendorContactEmail);
            txtContactFirst.Text = (SelectedVendor == null ? "" : SelectedVendor.vendorContactFirstName);
            txtContactLast.Text = (SelectedVendor == null ? "" : SelectedVendor.vendorContactLastName);
            txtContactPhone.Text = (SelectedVendor == null ? "" : SelectedVendor.vendorContactPhone);
            txtVendorAddress1.Text = (SelectedVendor == null ? "" : SelectedVendor.vendorAddress1);
            txtVendorAddress2.Text = (SelectedVendor == null ? "" : SelectedVendor.vendorAddress2);
            txtVendorCity.Text = (SelectedVendor == null ? "" : SelectedVendor.vendorCity);
            txtVendorLicenseNumber.Text = (SelectedVendor == null ? "" : SelectedVendor.vendorLicenseNumber);
            txtVendorName.Text = (SelectedVendor == null ? "" : SelectedVendor.vendorName);
            txtVendorZip.Text = (SelectedVendor == null ? "" : SelectedVendor.vendorZip);
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            cboVendorList.SelectedIndex = -1;
            SelectedVendor = null;
            loadVendorForm();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //check to see if new
            if (SelectedVendor == null)
            {
                SelectedVendor = new Vendor();
                dbConn.db.Vendors.InsertOnSubmit(SelectedVendor);
            }
            //save the current or new vendor
            SelectedVendor.vendorContactEmail = txtContactEmail.Text;
            SelectedVendor.vendorContactFirstName = txtContactFirst.Text;
            SelectedVendor.vendorContactLastName = txtContactLast.Text;
            SelectedVendor.vendorContactPhone = txtContactPhone.Text;
            SelectedVendor.vendorAddress1 = txtVendorAddress1.Text;
            SelectedVendor.vendorAddress2 = txtVendorAddress2.Text;
            SelectedVendor.vendorCity = txtVendorCity.Text;
            SelectedVendor.vendorLicenseNumber = txtVendorLicenseNumber.Text;
            SelectedVendor.vendorName = txtVendorName.Text;
            SelectedVendor.vendorZip = txtVendorZip.Text;
            //hard-code state
            SelectedVendor.vendorState = "CO";
            //save back to db
            dbConn.db.SubmitChanges();
            //give alert
            MessageBox.Show("Vendor Saved");
            //close
            this.Close();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();

        }

    }
}
